<!-- 用户登录界面 -->
<template>
  <div id="login">
    <div class="bg"></div>
    <el-row class="main-container">
      <el-col :lg="6" :xs="16" :md="10" :span="10">
        <div class="top">
          <i class="iconfont icon-kaoshi"></i><span class="title">毕业生就业系统</span>
        </div>
        <el-form
              class="fromBasePosition"
              label-width="100px"
              style="max-width: 460px"
            >
              <h2 style="text-align: center">欢迎回来</h2>
              <el-divider content-position="center">密码登录</el-divider>
              <el-form-item label="用户名" >
                <el-input v-model="formLabelAlign.username" placeholder="请输入用户名"/>
              </el-form-item>

              <el-form-item label="密码" >
                <el-input v-model="formLabelAlign.password" type="password" placeholder="请输入密码" show-password/>
              </el-form-item>


              <el-row type="flex" justify="center">
                <el-col style="text-align: center"> <el-button type="text" @click="toRegister">注册账号</el-button></el-col>
              </el-row>

              <div style="display: flex;justify-content: center">
                <el-button type="primary" @click="login1()" style="margin-bottom: 10px;width: 70%;">登录</el-button>
              </div>
            </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {mapMutations} from 'vuex'
import { jwtDecode } from "jwt-decode";
export default {
  name: "login",
  data() {
    return {
      role: 2,
      labelPosition: 'left',
      formLabelAlign: {
        username: '',
        password: '',
      }
    }
  },
  methods: {
    ...mapMutations(['setRoleAsUser','setRoleAsAdmin','setUser']),
    //用户登录请求后台处理
    login1(){
      this.$axios({
        url: '/api/user/login',
        method: 'post',
        data: {
          ...this.formLabelAlign
        }
      }).then(res=>{
          let resData = res.data
          // console.log(resData)
        //登录成功
          if(!resData.code){
             this.$cookies.set('token',resData.data)
             this.$cookies.set('username',this.formLabelAlign.username)
            try {
              // 解析令牌并获取载荷数据
              const decoded = jwtDecode(resData.data);
              // console.log(decoded.claims.roles)
              // console.log(decoded.claims.role); // 输出解码后的对象，其中包含用户信息和权限等
              // 你现在可以使用decoded对象中的信息来进行权限控制或其他操作
              if(decoded.claims.roles===1){
                this.setRoleAsUser()
                // console.log("login:"+this.$store.state.roles)
              }
              if(decoded.claims.roles===0){
                this.setRoleAsAdmin()
                // console.log("login:"+this.$store.state.roles)
              }
            } catch (error) {
              console.error('解析令牌时出错:', error);
              // 处理错误，例如重新登录或显示错误消息
            }

            //存储用户信息到store
            this.$axios({
              url:'/api/user/userInfo',
              method:'get',
              headers:{
                "Authorization":this.$cookies.get('token')
              }
            }).then(res =>{
              this.setUser(res.data.data)
              // console.log(res.data.data)
            })

            this.$router.push({path:'/index'})
          }
          if(resData.code){
            this.$message({
              showClose: true,
              type: 'error',
              message: resData.message
            })
          }
        })
      },
    toRegister(){
      this.$router.push({path:'/register'})

    }

  },
  computed:{

  }
}
</script>

<style lang="less" scoped>
.fromBasePosition{

  border: 1px solid #f6f6f6;
  padding: 35px 35px 15px 35px;
  background-color: #fff;
  border-radius: 15px;
  box-shadow: 0 0 25px #cac6c6;

}
.remind {
  border-radius: 4px;
  padding: 10px 20px;
  display: flex;
  position: fixed;
  right: 20px;
  bottom: 50%;
  flex-direction: column;
  color: #606266;
  background-color: #fff;
  border-left: 4px solid #409eff;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)
}
.container {
  margin-bottom: 32px;
}
.container .el-radio-group {
  margin: 30px 0px;
}
a:link {
  color:#8f6234;
  text-decoration:none;
}
#login {
  font-size: 14px;
  color: #000;
  background-color: #fff;
}
#login .bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  overflow-y: auto;
  height: 100%;
  background: url('../../assets/img/loginBg.png')center top / cover no-repeat;
  background-color: #b6bccdd1 !important;
}
#login .main-container {
  display: flex;
  justify-content: center;
  align-items: center;
}
#login .main-container .top {
  margin-top: 100px;
  font-size: 30px;
  color: #8f6234;
  display: flex;
  justify-content: center;
}
#login .top .icon-kaoshi {
  font-size: 80px;
}
#login .top .title {
  margin-top: 20px;
}
#login .bottom {
  display:flex;
  justify-content: center;
  background-color:#fff;
  border-radius: 5px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
#login .bottom .title {
  text-align: center;
  font-size: 30px;
}
.bottom .container .title {
  margin: 30px 0px;;
}
.bottom .submit .row-login {
  width: 100%;
  background-color: #04468b;
  border-color: #04468b;
  margin: 20px 0px 10px 0px;
  padding: 15px 20px;
}
.bottom .submit {
  display: flex;
  justify-content: center;
}
.footer {
  margin-top: 50px;
  text-align: center;
}
.footer .msg1 {
  font-size: 18px;
  color: #fff;
  margin-bottom: 15px;
}
.footer .msg2 {
  font-size: 14px;
  color: #e3e3e3;
  margin-top: 70px;
}
.bottom .options {
  margin-bottom: 40px;
  color: #ff962a;
  display: flex;
  justify-content: space-between;
}
.bottom .options > a {
  color: #ff962a;
}
.bottom .options .register span:nth-child(1) {
  color: #8C8C8C;
}
</style>
